//
//  LKEmoticonPageViewCell.swift
//  LKWeibo
//
//  Created by like on 16/8/18.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit

class LKEmoticonPageViewCell: UICollectionViewCell {
    
    // 定义一个属性(一维数组 8  20) 供外界调用
    var emoticons:[LKEmoticonModel]?{
        didSet{
            
            
            // 先全部隐藏(cell 复用问题)
            for button in buttonsArray {
                button.hidden = true
            }
            
            for (i,emoticonModel) in emoticons!.enumerate(){
                
                let button = buttonsArray[i]
                
                // 显示button(cell 复用问题)
                button.hidden = false

                if emoticonModel.isEmoji {
                    //用导入的分类 将16进制转emoji串
                    let code = (emoticonModel.code ?? "" as NSString).emoji()
                    button.setTitle(code, forState: UIControlState.Normal)
                    
                    // (cell 复用问题)
                    button.setImage(nil, forState: UIControlState.Normal)
                }else{
                    
                    button.setImage(UIImage.emoticonsImage(emoticonModel), forState: UIControlState.Normal)
                    // (cell 复用问题)
                    button.setTitle(nil, forState: UIControlState.Normal)
                }
                
            }
        
        }
    }
    
    var indexPath: NSIndexPath?{
        didSet{
            // 设置text
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
    }
    
    // 定义一个可变的空数组  用来放20个button
    var buttonsArray:[UIButton] = [UIButton]()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = RandomColor()
        //添加button
        addChildButton()
        // 添加控件
        contentView.addSubview(messageLabel)
        // 添加约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(contentView)
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        // 计算button的宽和高
        let w = self.frame.width/CGFloat(LKEmoticonsMaxCol)
        let h = self.frame.height/CGFloat(LKEmoticonsMaxRow)
        // 设置20个按钮的frame
        // 遍历buttonsArray
        for (index, button) in buttonsArray.enumerate() {
            // 设置frame
            let col = CGFloat(index%LKEmoticonsMaxCol)
            let row = CGFloat(index/LKEmoticonsMaxCol)
            button.frame = CGRect(x: col*w, y: row*h, width: w, height: h)
        }
    }
    
    //创建20个button
    private func addChildButton(){
        
        for _ in 0..<LKEmoticonsMaxCount {
            let button = UIButton()
            // 设置字体大小
            button.titleLabel?.font = UIFont.systemFontOfSize(30)
            // 设置随机颜色
            button.backgroundColor = RandomColor()
            // 添加button
            buttonsArray.append(button)
            // 添加button
            contentView.addSubview(button)
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35, maxWidth: SCREENW)
    
}
